package com.yztc.xiami_app.ui.shopcart.model;

import android.util.Log;

import com.yztc.xiami_app.bean.shopcart.ShopCart;
import com.yztc.xiami_app.ui.shopcart.interfaces.IGetShopCartDataListener;

import java.util.List;

import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;

/**
 * Created by 周旭 on 2016/11/21.
 * "获取购物车"商品信息的Model层的实现类
 */

public class ShopCartModelImpl implements IShopCartModel {

    private IGetShopCartDataListener listener;

    public ShopCartModelImpl(IGetShopCartDataListener listener) {
        this.listener = listener;
    }

    //获取Bmob云中的数据库中的购物车的商品信息的列表
    @Override
    public void getShopCartData() {

        BmobQuery<ShopCart> query = new BmobQuery<ShopCart>();
        query.findObjects(new FindListener<ShopCart>() {
            @Override
            public void done(List<ShopCart> list, BmobException e) {
                if (e == null) {
                    //把获取到的购物车的数据设置，交给Presenter来处理
                    listener.setData(list);
                } else {
                    Log.i("tag", "----------->查询失败");
                }
            }
        });
    }

    //从Bmob中删除,参数为删除的列表
    @Override
    public void deleteGoods(List<BmobObject> shopCartList) {

        new BmobBatch().deleteBatch(shopCartList).doBatch(new QueryListListener<BatchResult>() {
            @Override
            public void done(List<BatchResult> list, BmobException e) {
                if(e == null){
                    listener.setDeleteResult(true);
                }else {
                    listener.setDeleteResult(false);
                }
            }
        });
    }
}
